<?
require_once("cms/core/action/AbstractListAction.php");
require_once("cms/event/om/EventI18nPeer.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating events");
        $c = new Criteria();
        $c->add(EventI18nPeer::EVENT_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(EventI18nPeer::ACTIVE, true);
        try {
            EventI18nPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating events");
        $c = new Criteria();
        $c->add(EventI18nPeer::EVENT_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(EventI18nPeer::ACTIVE, false);
        try {
            EventI18nPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting events");

        $c = new Criteria();
        $c->add(EventI18nPeer::EVENT_I18N_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $eventI18ns = EventI18nPeer::doSelect($c);
            foreach($eventI18ns as $eventI18n) {
                // Delete the event itself
                if($eventI18n->getLanguageCode() ==
                        $request->getContext()->getInitParameter(_Core::DEFAULT_LANGUAGE)) {
                    $event = $eventI18n->getEvent();
                    // Delete the Event and have the onDelete=cascade take care of the rest.
                    EventPeer::doDelete($eventI18n->getEventId(), $conn);
                } else {
                    // Just this row.
                    EventI18nPeer::doDelete($eventI18n->getEventI18nId(), $conn);
                }
            }

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e);
        }

    }

}
?>